package com.wn.system.controller;

import com.wn.common.exception.Assert;
import com.wn.common.exception.BusinessException;
import com.wn.common.result.Result;
import com.wn.common.result.enums.IErrorCode;
import com.wn.common.result.enums.impl.BusinessCode;
import com.wn.common.result.enums.impl.ResultCode;
import com.wn.system.entity.User;
import com.wn.system.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author yellow docker
 * @since 2023-02-23
 */
@Api(tags = "test模块")
@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private IUserService userService;

    @GetMapping("test")
    public List<User> test() {
        return userService.list();
    }

    @GetMapping("test1")
    public Result test1() {
        return Result.ok();
    }

    @GetMapping("test2")
    public Result test2() {
        return Result.fail();
    }

    @GetMapping("test3/{id}")
    public Result test3(@PathVariable("id") Integer id) {

        if (id == 1) {
            return Result.fail(ResultCode.PARAM_TYPE_BIND_ERROR.getCode(), ResultCode.PARAM_TYPE_BIND_ERROR.getMessage());
        }

        if (id == 2) {
            return Result.fail(BusinessCode.USER_LOGIN_ERROR.getCode(), BusinessCode.USER_LOGIN_ERROR.getMessage());
        }

        return Result.fail();
    }

    @GetMapping("excep/{id}")
    public void excep(@PathVariable("id") Integer id) {

        if (id == 1) {
            throw new BusinessException("手动抛出异常...");
        }
        if (id == 2) {
            throw new IllegalArgumentException();
        }
        if (id == 3) {
            Assert.error(ResultCode.PARAM_IS_BLANK);
        }
        if (id == 4) {
            Object obj = null;
            Assert.error(obj, ResultCode.PARAM_IS_BLANK);
        }
    }

    //这个亲请求中必须要携带token才可以访问
    @ApiOperation("需要携带token可以访问,需要登录才可以访问")
    @GetMapping("/auth/test")
    public Result auth1(HttpServletRequest request) {

        String phone = request.getHeader("phone");
        System.out.println("phone = " + phone);

        //RequestContextHolder 来获取 HttpServletRequest
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest req = servletRequestAttributes.getRequest();
        String phone1 = req.getHeader("phone");
        System.out.println("phone1 = " + phone1);

        return Result.ok();
    }

    //不需要token才可以访问
    @ApiOperation("不需要携带token可以访问,不需要认证")
    @GetMapping("/aaa")
    public Result auth2() {
        return Result.ok();
    }
}